package com.lenovo.leos.filebrowser.netdisk;

import java.util.ArrayList;
import java.util.List;
import com.lenovo.leos.filebrowser.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/** @author Steven Osborn - http://steven.bitsetters.com */
public class LenovoInterfaceAdapter extends BaseAdapter{

	/** Remember our context so we can use it when constructing views. */
	private Context mContext;
	private LayoutInflater mInflater;

	private List<NetFileItem> mItems = null;
	private static final String TAG = "LenovoAdapter";
	boolean flag = false;
	int checkedPos;
	boolean mChecked = false;

	public LenovoInterfaceAdapter(Context context, List<NetFileItem> mItems) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		this.mItems = new ArrayList<NetFileItem>();

		if (mItems != null)
			this.mItems.addAll(mItems);
	}

	public void setListItems(List<NetFileItem> lit) {
		mItems.clear();

		if (lit != null)
			mItems.addAll(lit);
	}

	/** @return The number of items in the */
	public int getCount() {
		return mItems.size();
	}

	public Object getItem(int position) {
		return mItems.get(position);
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	/** Use the array index as a unique id. */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * @param convertView
	 *            The old view to overwrite, if one is passed
	 * @returns a IconifiedTextView that holds wraps around an NetFileItem
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final int pos = position;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.interface_listitem, null);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.position = pos;
			convertView.setTag(holder);

		} else {
			// Log.i(TAG, "&&&&&&&&&&&&&&&&&&&& position = "+position);
			holder = (ViewHolder) convertView.getTag();
			holder.position = pos;
		}

		String s = mItems.get(pos).getText();
		if (s.startsWith("/")) {
			s = s.replaceFirst("/", "");
		}

		holder.text.setText(s);
		holder.icon.setAdjustViewBounds(true);
		holder.icon.setImageDrawable(mItems.get(pos).getIcon());

		return convertView;
	}

	public class ViewHolder {
		ImageView icon;
		TextView text;
		int position;
	}
}
